lib.use(['element', 'table', 'form','laydate','tablePlug'], function () {
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var laydate=layui.laydate;
    var purchaseHeadTable;
    var searchFrm = layui.Form('searchFrm');

    table.set(common.layui.table.config());

    searchFrm.selectRender();
   // alert(common.util.getPathName())
    laydate.render({elem: '#purchaseDate' ,range: '至'});
    laydate.render({elem: '#checkDate' ,range: '至'});
    // 渲染表格

    purchaseHeadTable = table.render({
        elem: '#purchaseHeadTable'
        , url: common.util.createUrl('/pos/purchaseHead/list')
        , cols: [[
             {type:'checkbox' }
            ,{type: 'numbers', title: '编号'}
            ,{ title: '单据编码', field: 'purchaseNo' ,width:130 }
            ,{ title: '单据日期', field: 'purchaseDate',width:120 }
            ,{ title: '供应商名称', field: 'supplierName' }
            ,{ title: '采购员名称', field: 'guideName',width:100 }
            ,{ title: '总采购量', field: 'sumNum' ,width:80}
            ,{ title: '总入库量', field: 'inSumNum' ,width:80}
            ,{ title: '订单类型', field: 'purchaseType' ,templet: function (row) { return common.dist.getValue("pos.PurchaseHead_PurchaseTypeEnum",row.purchaseType);}}
            ,{ title: '订单状态', field: 'purchaseState' ,templet: function (row) { return common.dist.getValue("pos.PurchaseHead_PurchaseStateEnum",row.purchaseState);}}
            ,{ title: '审核人', field: 'checkUserName' }
            ,{ title: '审核时间', field: 'checkDate',width:150 }
            //,{ title: '创建人id', field: 'createUserId',  hide: true}
            //,{ title: '创建人', field: 'createUserName',  hide: true}
            ,{ title: '创建时间', field: 'createTime',width:150}
           // ,{ title: '修改人id', field: 'updateUserId',  hide: true}
            //,{ title: '修改人', field: 'updateUserName' }
           // ,{ title: '修改时间', field: 'updateTime' }
            ,{field: 'id',  hide: true }
            ,{field: 'entId',  hide: true}
            ,{field: 'id',  hide: true}
            ,{field: 'remark',   hide: true}
            ,{field: 'guideId',  hide: true}
            ,{field: 'checkUserId',  hide: true}
            ,{field: 'supplierId',  hide: true}
        ]]
    });


    /**
     * 监听行单击事件，进入编辑事件
     */
    table.on('rowDouble(purchaseHeadTableFilter)', function(obj){
        update(obj)
    });
    /**
     * 删除
     */
    $(document).on('click','#del',function(){
        del(table.checkStatus('purchaseHeadTable'))
    });
    /**
     * 审核
     */
    $(document).on('click','#check',function(){
        check(table.checkStatus('purchaseHeadTable'))
    });
    /**
     * 新增
     */
    $(document).on('click','#add',function(){
        var config = {
            title:'新增采购订单',
            content: 'pos/purchaseHead/purchaseHeadForm.html'
        }
        common.util.openWindow(busCommon.windowConfig(config))
    });
    /**
     * 导出
     */
    $(document).on('click','#export',function(){
        var params=common.from.serializeObject('#searchFrm');
        params['pages']=purchaseHeadTable.config.page.pages;
        params['limit']=purchaseHeadTable.config.page.limit;
        common.util.exportWindows('/pos/purchaseHead/export',params)
    });

    form.on('submit(searchFilter)', function(data){
        var param = data.field;
        searchTable(param)
        return false;
    });


    /**
     * 查询表格
     * @param params
     */
    function searchTable(params) {
       params=common.from.serializeObject('#searchFrm');
       purchaseHeadTable=table.reload('purchaseHeadTable',{where:params,page: {curr:1}})
    }
    /**
     * 修改
     * @param obj
     */
    function update(obj) {
        if(!common.auth.hasPermission("pos:purchaseHead:update")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }
        if(postData.length!=1)return;
        var config = {
            title:'修改采购单',
            content: 'pos/purchaseHead/purchaseHeadForm.html?id=' + postData[0]
        }
        common.util.openWindow(busCommon.windowConfig(config))
    }
    /**
     * 删除
     * @param obj
     */
    function del(obj) {
        if(!common.auth.hasPermission("pos:purchaseHead:delete")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item;
            });
        }else{
            postData=[obj.data]
        }

        if(postData.length!=1){common.util.error('请选择一条记录进行操作');return;}
        /*if(postData[0].orderState!=1){
           return  common.util.error('[{0}] 当前单据非未审核状态,禁止删除',format(postData[0].probNo));
        }*/
        common.util.confirm('确认删除!', function(index){
            console.log('Config Del param is', postData);
            common.ajax.get('/pos/purchaseHead/delete/'+postData[0].id,null, function (resp) {
                searchTable();
                layer.close(index);
            })

        });
    }

    /**
     * 审核
     * @param obj
     */
    function check(obj) {
        if(!common.auth.hasPermission("pos:purchaseHead:check")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item;
            });
        }else{
            postData=[obj.data]
        }

        if(postData.length!=1){common.util.error('请选择一条记录进行操作');return;}
        common.util.confirm('确认审核!', function(index){
            console.log('Config Check param is', postData);
            common.ajax.get('/pos/purchaseHead/check/'+postData[0].id, null, function (resp) {
                searchTable();
                layer.close(index)
                common.util.success("审核成功");
            })

        });
    }
    window.formCallBack=function () {
        searchTable();
    }
});
